import { LOGIN, LOGOUT } from '.././mutation_types'

// localStorage 保存数据时间更久，不会因为关闭标签而丢失

const state = {
    tenant_id: localStorage.getItem('tenant_id') || 0,
    tenant_name: localStorage.getItem('tenant_name') || '未登录',
    ttoken: localStorage.getItem('ttoken') || ''
}

const getters = {
    tenant_id: state => {
        return state.tenant_id
    },
    tenant_name: state => {
        return state.tenant_name
    },
    ttoken: state => {
        return state.ttoken
    }
}

const mutations = {
    [LOGIN](state, data) {
        localStorage.setItem('tenant_id', data.id); // 设置localStorage
        localStorage.setItem('tenant_name', data.name);
        localStorage.setItem('ttoken', data.token);
        state.user_id = data.id;
        state.user_name = data.name;
        state.ttoken = data.token;
    },
    [LOGOUT](state) {
        localStorage.removeItem('tenant_id'); // 移除localStorage
        localStorage.removeItem('tenant_name');
        localStorage.removeItem('ttoken');
        state.tenant_id = 0
        state.tenant_name = '';
        state.ttoken = '';
    }
}

export default {
    state,
    mutations,
    getters
}
